package com.msg.service.Impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.msg.aliyun.msm.AliyunSmsBase;
import com.msg.service.SampleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author lxx
 */
@Service
public class SampleServiceImpl implements SampleService {

    @Value("${aliyun.accessKey}")
    private String accessKey;

    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    /**
     *
     * @param phone 手机
     * @param param 模板参数
     * @param templateCode 模板代码
     * @param signName 签名
     * @return boolean
     */
    @Override
    public boolean sendSms(String phone, String param, String templateCode,String signName) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                //手机号
                .setPhoneNumbers(phone)
                //签名
                .setSignName(signName)
                //短信模板
                .setTemplateCode(templateCode)
                //模板参数
                .setTemplateParam(param);
        RuntimeOptions runtime = new RuntimeOptions();
        String code = null;
        try {
            Client client = AliyunSmsBase.createClient(accessKey,accessKeySecret);
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            code = response.getBody().getCode();
        }catch (Exception e){
            log.error("调用阿里云短信服务异常！异常信息：" + e.getMessage());
        }
        return "OK".equals(code);
    }

    /**
     *
     * @param phone 手机
     * @param param 模板参数
     * @param templateCode 模板代码
     * @param signName 签名
     * @return boolean
     */
    @Override
    public boolean sendBatchSms(String phone, String param, String templateCode, String signName) {
        SendBatchSmsRequest sendBatchSmsRequest = new SendBatchSmsRequest()
                //手机号
                .setPhoneNumberJson(phone)
                //签名
                .setSignNameJson(signName)
                //短信模板
                .setTemplateCode(templateCode)
                //模板参数
                .setTemplateParamJson(param);
        RuntimeOptions runtime = new RuntimeOptions();
        String code = null;
        try {
            Client client = AliyunSmsBase.createClient(accessKey,accessKeySecret);
            SendBatchSmsResponse response = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
            code = response.getBody().getCode();
            log.info(response.getBody().getMessage());
        }catch (Exception e){
            log.error("调用阿里云批量发送短信服务异常！异常信息：" + e.getMessage());
        }
        return "OK".equals(code);
    }


}
